/*
 * Copyright (c) 2010, Jim Hollinger
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Jim Hollinger nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <time.h>

#include "debughelper/debughelper.h"
#include "nmea0183message.h"

#if !defined (KNOTS_TO_METERS_PER_SECOND)
#  define KNOTS_TO_METERS_PER_SECOND     (0.514444444F)
#endif


using namespace nmea0183;


Nmea0183Message::Nmea0183Message() : messageType("base") {}

Nmea0183Message::~Nmea0183Message() {}

bool Nmea0183Message::isMessageType(const char *targetType) {
    return (strncmp(targetType, messageType, NMEA0183_MSG_TYPE_LEN) == 0);
}

// hhmmss.s
bool Nmea0183Message::nmeaUtcTimeToSeconds(const char *param1, double *time_s) {
    bool flag = false;

    int i = 0;
    for (i = 0; i < 6; i++) {
        if ((param1[i] == '.') || (param1[i] == '\0')) {
            break;
        }
    }
    int    hours   = 0;
    int    minutes = 0;
    double seconds = 0.0;
    if (i == 6) {
        hours   = ((param1[0] - '0') * 10) + (param1[1] - '0');
        minutes = ((param1[2] - '0') * 10) + (param1[3] - '0');
        seconds = strtod(&param1[4], NULL);
        flag = true;
    } else if (i == 5) {
        hours = param1[0] - '0';
        minutes = ((param1[1] - '0') * 10) + (param1[2] - '0');
        seconds = strtod(&param1[3], NULL);
        flag = true;
    }
    if (flag) {
        seconds += minutes * 60.0;
        seconds += hours * 3600.0;

        time_t now_t = time(NULL);
        struct tm now_tm;
        gmtime_s(&now_tm, &now_t);
        now_tm.tm_sec  = 0;
        now_tm.tm_min  = 0;
        now_tm.tm_hour = 0;
        time_t midnight_t = _mkgmtime(&now_tm);
        *time_s = midnight_t + seconds;
    }

    return flag;
}

// ddmm.mm (llll.ll)
bool Nmea0183Message::nmeaLatitudeToDegN(const char *param1,
                                         const char *param2,
                                         double *degN) {
    bool flag = true;

    double degrees = ((param1[0] - '0') * 10) + (param1[1] - '0');
    double minutes = strtod(&param1[2], NULL);
    *degN = degrees + (minutes / 60.0);
    if (param2[0] == 'S') {
        *degN = -*degN;
    }

    return flag;
}

// dddmm.mm (yyyyy.yy)
bool Nmea0183Message::nmeaLongitudeToDegE(const char *param1,
                                          const char *param2,
                                          double *degE) {
    bool flag = true;

    double degrees = ((param1[0] - '0') * 100) +
                     ((param1[1] - '0') * 10) +
                      (param1[2] - '0');
    double minutes = strtod(&param1[3], NULL);
    *degE = degrees + (minutes / 60.0);
    if (param2[0] == 'W') {
        *degE = -*degE;
    }

    return flag;
}

bool Nmea0183Message::nmeaDistanceToMeters(const char *param1,
                                           double *distance_meters) {
    bool flag = true;

    switch (param1[0]) {

    case 'K':   // kilometers/hour
        *distance_meters *= (1000.0 / 3600.0);
        break;

    case 'M':   // meters
        break;

    case 'N':   // knots
        *distance_meters *= KNOTS_TO_METERS_PER_SECOND;
        break;

    case '\0':  // not present
        *distance_meters = 0.0;
        break;

    default:
        flag = false;
        break;
    }

    return flag;
}
